var roleId;
$.ajax({
    url: "/recruitmentManager/getRoleIdOfLoginUser",
    type: "POST",
    async:false,
    success: function (result) {
        if (result.code == 0) {
            roleId = result.roleIdOfLoginUser;
            $("#getRoleIdOfLoginUser").val(roleId);  //往input框里传值
            console.log("roleId的值:"+$("#getRoleIdOfLoginUser").val());
        } else {
            layer.alert(result.msg);
        }
    }
});

$(function(){
    $('#selectAllRecruitTable').bootstrapTable({
        url: "/recruitmentManager/selectAllRecruit",
        //data: JSON.stringify(institutionId),//参数
        method: 'post',
/*        queryParams: function (params) {
            return {
                institutionId: institutionId
            };

        },*/
        showRefresh: true,  //显示刷新按钮
        search: true,
        toolbar:'#toolbar',
        // sortable: true,                     //是否启用排序
        // sortOrder: "asc",                   //排序方式
        pageList: [3, 5, 10, 20],        //可供选择的每页的行数（*）
        striped : true,     //设置为true会有隔行变色效果
        //sidePagination:'server',//服务器端分页默认接受rows和total
        pagination:true,//客户端分页
        sidePagination:'client',
        idFiled:'recruit_id',//必选属性
        pageNumber:1,
        pageSize:5,
        columns: [
            {
                field: 'checkId',
                checkbox:true,
                clickToSelect:true
            },{
                field: 'recruit_id',
                title: '招聘信息id',
                visible:false

            },{
                field: 'release_date',
                title: '发布状态',
                formatter: function (release_date) {
                    if(release_date){
                        return "已发布";
                    }else{
                        return "未发布";
                    }
                }
            },{
                field: 'iname',
                title: '机构'
            },{
                field: 'pname',
                title: '职位'
            },{
                field: 'recruit_num',
                title: '招聘人数'
            },{
                field: 'start_date',
                title: '开始日期',
                formatter: function (start_date) {
                    var tmpDate = new Date(start_date);
                    var yy = tmpDate.getFullYear();

                    var m = tmpDate.getMonth() + 1;

                    var dd = tmpDate.getDate();

                    var hh = tmpDate.getHours();

                    var mm = tmpDate.getMinutes();

                    var ss = tmpDate.getSeconds();

                    return yy + '-' + m + '-' + dd;
                }
            },{
                field: 'stop_date',
                title: '结束日期',
                formatter: function (stop_date) {
                    var tmpDate = new Date(stop_date);
                    var yy = tmpDate.getFullYear();

                    var m = tmpDate.getMonth() + 1;

                    var dd = tmpDate.getDate();

                    var hh = tmpDate.getHours();

                    var mm = tmpDate.getMinutes();

                    var ss = tmpDate.getSeconds();

                    return yy + '-' + m + '-' + dd;
                }
            },{
                field: 'claim',
                title: '招聘简介'
            }]
    });
});

var vm = new Vue({
    el: '#publishRecruitment',
    data: {
        showList: 1,
        recruitId: '',
        title: '',
        //选课弹窗需要字段
        institutionName:'',
        positionName:'',
        recruitNum: '',//招聘人数
        startDate:'',
        stopDate: '',
        claim:'',

    },
    methods: {
        //删除
        del: function () {
            var rows = $("#selectAllRecruitTable").bootstrapTable('getSelections');

            if (rows.length == 0) {
                layer.alert('请选择至少一条记录');
                return;
            } else {
                layer.confirm('您确定删除所选的招聘信息吗？', {
                    btn: ['确定', '取消'] //可以无限个按钮
                }, function () {
                    var ids = new Array();
                    //遍历所有选择的行数据，取每条数据对应的ID
                    $.each(rows, function (i, row) {
                        ids[i] = row['recruit_id'];
                    });

                    $.ajax({
                        type: "POST",
                        url: "/recruitmentManager/deleteRecruitByIds",
                        data: JSON.stringify(ids),
                        success: function (r) {
                            if (r.code === 0) {
                                layer.alert('删除成功');
                                $('#selectAllRecruitTable').bootstrapTable('refresh');
                            } else {
                                layer.alert(r.msg);
                            }
                        },
                        error: function () {
                            layer.alert('服务器没有返回数据，可能服务器忙，请重试');
                        }
                    });
                });
            }
        },
        //发布
        publish: function () {
            var row = $("#selectAllRecruitTable").bootstrapTable('getSelections');
            var RoleId = $("#getRoleIdOfLoginUser").val();
            console.log("RoleId的值:"+$("#getRoleIdOfLoginUser").val());
            if(RoleId != 1){
                layer.alert('权限不足！');
                return;
            }else if (row.length === 0) {
                layer.alert('请选择一条记录');
                return;
            } else if (row.length > 1) {
                layer.alert("只能选择一条记录");
                return;
            } else {
                var selectedId = row[0].recruit_id;
                console.log(selectedId);
                var status = row[0].release_date;
                console.log(status);
                if(status){
                    layer.alert("该招聘信息已发布！");
                    return;
                }else{
                    // var date = new Date();
                    // var year = date.getFullYear();
                    // var month = date.getMonth() + 1;
                    // var day = date.getDate();
                    // if (month < 10) {
                    //     month = "0" + month;
                    // }
                    // if (day < 10) {
                    //     day = "0" + day;
                    // }
                    // status = year + "-" + month +"-" + day;
                    console.log(status);
                    // var date = new Date(status);
                    // var nowDate = date.toLocaleDateString();
                    var data1 = {"recruitId":selectedId};
                    console.log(data1);

                    $.ajax({
                        type: "POST",
                        url: "/recruitmentManager/publishRecruit",
                        data: JSON.stringify(data1),
                        success: function (r) {
                            if (r.code === 0) {
                                layer.alert('发布成功');
                                $('#selectAllRecruitTable').bootstrapTable('refresh');
                            } else {
                                layer.alert(r.msg);
                            }
                        },
                        error: function () {
                            layer.alert('服务器没有返回数据，可能服务器忙，请重试');
                        }
                    });
                }

                }
            },
        //更新招聘信息
        update: function () {
            var row = $("#selectAllRecruitTable").bootstrapTable('getSelections');
            if (row.length === 0) {
                layer.alert('请选择一条记录');
                return;
            } else if (row.length > 1) {
                layer.alert("只能选择一条记录");
                return;
            } else {
                $('#updateRecruitDialog').modal("show");
                var selectedId = row[0].recruit_id;
                console.log(selectedId);

                //回显需要修改招聘信息
                $.ajax({
                    type: "POST",
                    url: "/recruitmentManager/selectRecruitById",
                    data: JSON.stringify(selectedId),
                    success: function (result) {
                        var data = result.RecruitById;
                        if (result.code === 0) {
                            console.log(result);
                            vm.recruitId = data[0]['recruit_id'];
                            vm.institutionName = data[0]['iname'];
                            vm.positionName = data[0]['pname'];
                            vm.recruitNum = data[0]['recruit_num'];
                            var tmpDate = new Date(data[0]['start_date']);
                            var yy = tmpDate.getFullYear();
                            var m = tmpDate.getMonth() + 1;
                            var dd = tmpDate.getDate();
                            if(m<=9){
                                m = '0'+m;
                            }
                            if(dd<=9){
                                dd = '0'+dd;
                            }
                            var startDate=yy + '-' + m + '-' + dd;
                            vm.startDate = startDate;
                            console.log(vm.startDate);
                            var tmpDate1 = new Date(data[0]['stop_date']);
                            var yy = tmpDate1.getFullYear();
                            var m = tmpDate1.getMonth() + 1;
                            var dd = tmpDate1.getDate();
                            if(m<=9){
                                m = '0'+m;
                            }
                            if(dd<=9){
                                dd = '0'+dd;
                            }
                            var stopDate=yy + '-' + m + '-' + dd;
                            console.log(stopDate);
                            vm.stopDate = stopDate;
                            console.log(vm.stopDate);
                            vm.claim = data[0]['claim'];
                        } else {
                            layer.alert(result.msg);
                        }
                    }
                });
            }
        },
        //更新提交
        submitUpdate: function () {
            //获取下拉列表所选的value
            // var courseId = $("#courseName option:selected").val();
            var row = $("#selectAllRecruitTable").bootstrapTable('getSelections');
            var selectedId = row[0].recruit_id;
            var role3={"recruitNum":vm.recruitNum,"startDate":vm.startDate,"stopDate":vm.stopDate,
                "claim":vm.claim,"recruitId":selectedId};
            console.log(selectedId);
            console.log(JSON.stringify(role3));
            layer.confirm("确定更新角色吗？", {
                btn: ["确定", "取消"]
            }, function () {
                $.ajax({
                    type: "POST",//post方法默认是表单数据提交，用requestParameter接收，如果要用jason需要配置在conmmon.js中的第一行有配置
                    url: '/recruitmentManager/updateRecruit',//请求的controller
                    data: JSON.stringify(role3),//参数
                    success: function (r) {
                        if (r.code === 0) {
                            // layer.close(index);
                            layer.alert('修改成功', function () {
                                location.reload();
                            });
                            $('#selectAllRecruitTable').bootstrapTable('refresh');
                            //删除下拉框中当前选中的元素
                            // $("#courseName option:selected").remove();
                            //更新表格
                            // $('#selectedCoursesTable').bootstrapTable('refresh');
                            // $("#updateCourseDialog").bootstrapTable('refresh');
                        }
                    },
                    // error: function () {
                    //     layer.alert('服务器没有返回数据，可能服务器忙，请重试');
                    // }
                });
            });

        }
    }
});

